package 栈和队列;

import 抽象数据类型.ListNode;

import java.util.EmptyStackException;

public class LLStack {
     //
    private ListNode first;


    public static LLStack initStack(Object[] arr) {
        LLStack stack = new LLStack();
        for (Object o : arr) {
            stack.push((int)o);
        }
        return stack;
    }
    public LLStack() {
        first = new ListNode(Integer.MAX_VALUE);
    }

    public void push(int data) {
        if (first == null) {
            first = new ListNode(data);
        } else if (first.getVal() == Integer.MAX_VALUE) {
            first.setVal(data);
        }else {
            ListNode insertNode = new ListNode(data);
            insertNode.next = first;
            first = insertNode;
        }
    }

    public int top() {
        if (first == null) {
            throw new EmptyStackException();
        }else {
            return first.getVal();
        }
    }

    public int pop() {
        if (first == null) {
            throw new EmptyStackException();
        }else {
            int data = first.getVal();
            first = first.next;
            return data;
        }
    }

    public boolean isEmpty() {
        if (first == null) {
            return true;
        }else {
            return false;
        }
    }

    public void deleteStack() {
        first = null;
    }
    static  class Main{
        public static void main(String[] args) {
            Object[] a = {1, 3, 4, 5, 9};
            LLStack stack = LLStack.initStack(a);
            System.out.println(stack.top());
        }
    }
}
